﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB50AV95DataIN : BaseFunction
    {
        #region -------------function -------------------------
        public int ThreadCount;
        public AB50AV95DataIN()
            : base()
        {

        }
        public AB50AV95DataIN(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public AB50AV95DataIN(OracleConnection _OracleDB, IntexReptServBLClient _proxy)
            : base(_OracleDB, _proxy)
        {

        }
        ~AB50AV95DataIN()
        {
            if (OracleDB != null)
            {
                OracleDB.Close();
            }
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();

                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                string ABPART = dwABPOS["ABPART"].ToString();
                //string ABPLIEFNR = dwABPOS["LSNAME1"].ToString().Trim();
                or1.CommandText = "SELECT P.ABPAUFKRS,P.ABPAUFLFD,P.ABPAUFPOS,P.ABPART, P.ABPVAR, P.ABPFARBE,"
                    + " '#' AS GAART,'#' AS GAVAR,'#' AS GAFARBE,P.ABPSOLLMENGE1,P.ABPSOLLMENGE5, "
                    + " P.ABPSOLLMENGE8,P.ABPLIEFDAT,P.ABPSTARTDAT,B.GSAGARNNR AS HLTNUM,P.ABPERLKZ,P.ABPERLDAT"
                    + " FROM ABPOS P LEFT JOIN GSDAT B "
                    + " ON P.ABPMANDANT = B.GSMANDANT AND P.ABPART = B.GSART AND P.ABPVAR = B.GSVAR "
                    + " WHERE P.ABPMANDANT='00' AND ABPERLKZ=:ABPERLKZ"
                + " AND ABPAUFKRS LIKE '%" + ABPAUFKRS
                + "%' AND ABPAUFLFD LIKE '%" + ABPAUFLFD
                + "%' AND ABPAUFPOS LIKE '%" + ABPAUFPOS + "%' AND ABPART LIKE '%" + ABPART + "%' "
                + " ORDER BY  1,2,3";
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();

                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ABPSOLLMENGE5");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["ABPFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["ABPFARBE"] = value;
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        /*public override DataSet GetVKPOS(DataRow dwABPOS)
        {
            DataSet setLocal1 = base.GetVKPOS(dwABPOS);
            string ABAVKAUFTYP = "";
            string LSNAME1 = "";
            string sTemp = "";
            if (setLocal1.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow dwNEW in setLocal1.Tables["ABAUFM"].Rows)
                {
                    ABAVKAUFTYP = dwNEW["ABAVKAUFTYP"].ToString().Trim();
                    if (ABAVKAUFTYP == "K")
                    {
                        dwABPOS["VKPAUFKRS"] = dwNEW["ABAVKAUFKRS"];
                        dwABPOS["VKPAUFLFD"] = dwNEW["ABAVKAUFLFD"];
                        dwABPOS["VKPAUFPOS"] = dwNEW["ABAVKAUFPOS"];
                        dwABPOS["KDKUNDNA"] = dwNEW["KDKUNDNA"];
                        dwABPOS["ABARESQTY"] = dwNEW["ABARESQTY"];
                        dwABPOS["VKPMENGE1"] = dwNEW["VKPMENGE1"];
                        dwABPOS["STDBATCHNO"] = dwNEW["STDBATCHNO"];
                        dwABPOS["VKPWUTERM"] = dwNEW["VKPWUTERM"];
                        Tracer.Verbose(" 留位到客单", dwABPOS["VKPAUFKRS"].ToString() + "." + dwABPOS["VKPAUFLFD"].ToString() + "." + dwABPOS["VKPAUFPOS"].ToString());
                        
                    }
                }
            }
            return setLocal1;
        }*/
        private DataSet GetYARNUSAGE(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT P.ABPAUFKRS||P.ABPAUFLFD||P.ABPAUFPOS AS YUORDER"
                    + ",P.ABPAUFART AS YUORDERTYPE,ABARESMG AS MUQUANTITY"
                    + ",LSLIEFNA,LSNAME1 AS TPRODUCER"
                + "  FROM ABAUFM M LEFT JOIN ABPOS P ON P.ABPAUFKRS=M.ABAVKAUFKRS AND P.ABPAUFLFD=M.ABAVKAUFLFD AND P.ABPAUFPOS=M.ABAVKAUFPOS"
                + " LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS"
                + " JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR AND F.ABFMANDANT =L.LSMANDANT "
                + " WHERE  M.ABAAPOS>'0001' AND P.ABPAUFART in('DI','PR','VW')  "
                + " AND M.ABAAUFKRS=:ABPAUFKRS AND M.ABAAUFLFD=:ABPAUFLFD AND M.ABAAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("MUQUANTITY");
                    DivideBy1000(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string sTemp = "", YARNUSAGE = "", GPRODUCER = "", SPRODUCER = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["TPRODUCER"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["TPRODUCER"] = value;
                        sTemp = dwTemp["YUORDERTYPE"].ToString().Trim();
                        if (sTemp == "PR" || sTemp == "DI")
                        {
                            sTemp = dwTemp["MUQUANTITY"].ToString().Trim();
                            YARNUSAGE += "纬纱:" + sTemp + ";";
                            sTemp = dwTemp["TPRODUCER"].ToString().Trim() + ";";
                            if (!GPRODUCER.Contains(sTemp)
                          && sTemp != ";")
                            {
                                GPRODUCER += sTemp;
                            }

                        }
                        if (sTemp == "VW")
                        {
                            sTemp = dwTemp["MUQUANTITY"].ToString().Trim();
                            YARNUSAGE += "经纱:" + sTemp + ";";
                            sTemp = dwTemp["TPRODUCER"].ToString().Trim() + ";";
                            if (!SPRODUCER.Contains(sTemp)
                                && sTemp != ";")
                            {
                                SPRODUCER += sTemp;
                            }
                        }
                    }

                    YARNUSAGE += "***";
                    YARNUSAGE = YARNUSAGE.Replace(";***", "");
                    YARNUSAGE = YARNUSAGE.Replace("***", "");
                    YARNUSAGE = YARNUSAGE.Replace(";", Environment.NewLine);
                    dwABPOS["YARNUSAGE"] = YARNUSAGE;
                    SPRODUCER += "***";
                    SPRODUCER = SPRODUCER.Replace(";***", "");
                    SPRODUCER = SPRODUCER.Replace("***", "");
                    SPRODUCER = SPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["SPRODUCER"] = SPRODUCER;
                    GPRODUCER += "***";
                    GPRODUCER = GPRODUCER.Replace(";***", "");
                    GPRODUCER = GPRODUCER.Replace("***", "");
                    GPRODUCER = GPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["GPRODUCER"] = GPRODUCER;
                    Tracer.Verbose(ABPOS + " 纱线用途", YARNUSAGE);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetMATYARNINFO(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                /*
                  SELECT GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7 FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS
WHERE GAAUFKRS='PR' AND  GAAUFLFD='100428' AND GAAUFPOS='010'
                 */
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7"
                + " FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS"
                + " WHERE GAAUFKRS=:ABPAUFKRS AND  GAAUFLFD=:ABPAUFLFD AND GAAUFPOS=:ABPAUFPOS AND GASA = '713' ";

                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);
                int weftcount = 0;
                string sTemp = "", GWARPYARNINFO = "";
                decimal GAKG1 = 0, GAKG3 = 0, GAKG5 = 0, GAKG6 = 0;
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "GAAUF";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GAKG1");
                    Q100.Enqueue("GAKG3");
                    Q100.Enqueue("GAKG5");
                    Q100.Enqueue("GAKG6");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["GAFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["GAFARBE"] = value;
                        weftcount++;
                        sTemp = dwTemp["GAKG1"].ToString().Trim();
                        GAKG1 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG3"].ToString().Trim();
                        GAKG3 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG5"].ToString().Trim();
                        GAKG5 = System.Convert.ToDecimal(sTemp);
                        sTemp = dwTemp["GAKG6"].ToString().Trim();
                        GAKG6 = System.Convert.ToDecimal(sTemp);
                        if (GAKG1 > GAKG5)
                        {
                            sTemp = "";
                            sTemp += dwTemp["GAART"].ToString().Trim() + ".";
                            sTemp += dwTemp["GAVAR"].ToString().Trim() + ".";
                            sTemp += dwTemp["GAFARBE"].ToString().Trim();
                            sTemp += "总需求" + GAKG1.ToString() + "kg,";
                            sTemp += "已留位" + GAKG3.ToString() + "kg,";
                            sTemp += "已发纱" + GAKG5.ToString() + "kg,";
                            sTemp += "已消耗" + GAKG6.ToString() + "kg.";
                            if (GAKG1 - GAKG3 - GAKG5 > 0)
                            {
                                GWARPYARNINFO += sTemp + "待料@" + (GAKG1 - GAKG3 - GAKG5).ToString() + "kg;";
                            }
                            else
                            {
                                GWARPYARNINFO += sTemp + ";";
                            }
                        }
                    }
                    GWARPYARNINFO += "***";
                    GWARPYARNINFO = GWARPYARNINFO.Replace(";***", "");
                    GWARPYARNINFO = GWARPYARNINFO.Replace("***", "");
                    //GWARPYARNINFO = GWARPYARNINFO.Replace(";", Environment.NewLine);
                    if (GWARPYARNINFO.Contains("待料"))
                    {
                        GWARPYARNINFO = "共" + weftcount.ToString() + "原纱，其中" + GWARPYARNINFO;
                    }
                    else
                    {
                        GWARPYARNINFO = "共" + weftcount.ToString() + "原纱都已到位。";
                    }
                    dwABPOS["GWARPYARNINFO"] = GWARPYARNINFO;
                    Tracer.Verbose(" 原料信息", GWARPYARNINFO);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetABFOLGE(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region code reference
                /*
                  SELECT DISTINCT LSLIEFNA,LSNAME1,ABFMNRKRS||ABFMLFDNR||ABFMPOS AS GLOOMNUM FROM ABFOLGE F,LIEFDAT L  WHERE ABFMANDANT='00'
AND   F.ABFAUFKRS='PR' AND  ABFAUFLFD='100428' AND  ABFAUFPOS='010'  AND F.ABFLIEFNR=L.LSLIEFNR
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT DISTINCT LSLIEFNA,LSNAME1"
                + " FROM ABFOLGE F,LIEFDAT L "
                + " WHERE ABFMANDANT='00'  AND  ABFAUFKRS=:ABFAUFKRS AND ABFAUFLFD=:ABFAUFLFD AND ABFAUFPOS=:ABFAUFPOS "
                + " AND F.ABFLIEFNR=L.LSLIEFNR";

                or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";

                    byte[] b;
                    string value;
                    string sTemp = "", YPRODUCER = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["LSNAME1"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["LSNAME1"] = value;
                        sTemp = value + ";";
                        if (!YPRODUCER.Contains(sTemp)
                             && sTemp != ";")
                        {
                            YPRODUCER += sTemp;
                        }
                    }
                    YPRODUCER += "***";
                    YPRODUCER = YPRODUCER.Replace(";***", "");
                    YPRODUCER = YPRODUCER.Replace("***", "");
                    YPRODUCER = YPRODUCER.Replace(";", Environment.NewLine);
                    dwABPOS["YPRODUCER"] = YPRODUCER;
                    Tracer.Verbose(ABPOS + " 染纱厂", YPRODUCER);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        #endregion
        public override void BuildABPOS(DataRow dwABPOS)
        {
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();

            try
            {
                dwNABPOS.ItemArray = dwABPOS.ItemArray;
                DataSet set4Return = new DataSet();
                ABPOS = dwNABPOS["ABPAUFKRS"].ToString() + "." + dwNABPOS["ABPAUFLFD"].ToString() + "." + dwNABPOS["ABPAUFPOS"].ToString();
                ABPOS = ThreadCount.ToString() + ": " + ABPOS;
                string ABAVKAUFTYP = "";
                string LSNAME1 = "";
                string sTemp = "";

                #region----------------------- 查询客单----------------------------
                //GetVKPOS(dwNABPOS);
                #endregion

                #region----------------------- 查询染纱厂----------------------------
                GetABFOLGE(dwNABPOS);
                #endregion

                #region----------------------- 纱线用途----------------------------
                GetYARNUSAGE(dwNABPOS);
                #endregion

                #region-----------------------原纱信息----------------------------
                GetMATYARNINFO(dwNABPOS);
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                try
                {
                    dwABPOS.ItemArray = dwNABPOS.ItemArray;
                    string progress = "查询进展:[" + ThreadID.ToString() + "] 完成<-FinishedJobs->/<-TotalJobs->";
                    EventDispatcher.Instance.RaiseStatus(progress, StatusEventType.StartProgress);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ABPOS + ex.StackTrace, ex);
                }
            }
        }
    }
}